﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using HCDV;
using HCDV.HCSP;
using hrainwebbase.basecs;

namespace hrainwebbase.ashx
{
    /// <summary>
    /// sp 的摘要说明
    /// </summary>
    public class webappapi : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        //用户ID，用户机构ID，用户编号，用户机构编号，用户角色编号
        /// <summary>
        /// 回调动作代码
        /// </summary>
        protected string callBackAction
        {
            get
            {
                if (GetParam("hcac") == null)
                    return string.Empty;
                else
                    return GetParam("hcac");
            }
        }
        public enum RequestType
        {
            Auto,
            Form,
            QueryString,
            Cookies
        }
        protected string GetParam(string name, RequestType requestType = RequestType.Auto)
        {
            if ((requestType == RequestType.Auto || requestType == RequestType.Form) && !string.IsNullOrEmpty(Mcontext.Request.Form[name]))
                return Mcontext.Request.Form[name];
            if ((requestType == RequestType.Auto || requestType == RequestType.QueryString) && !string.IsNullOrEmpty(Mcontext.Request.QueryString[name]))
                return Mcontext.Request.QueryString[name];
            if ((requestType == RequestType.Auto || requestType == RequestType.Cookies) && Mcontext.Request.Cookies[name] != null)
                return Mcontext.Request.Cookies[name].Value;
            return string.Empty;
        }
        protected HttpContext Mcontext;
        public void ProcessRequest(HttpContext context)
        {
            Mcontext = context;
            Mcontext.Response.ContentType = "application/Json";
            Mcontext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            HttpRequest Request = Mcontext.Request;
            switch (callBackAction)
            {
                case "getpage":
                    {
                        try
                        {
                            //目前只支持如下内容
                            //select[]table[]group[]having[]where[]order[]  
                            //hcaddhc 代表+
                            string select = GetParam("select");
                            string table = GetParam("table");
                            string where = GetParam("where");
                            string group = GetParam("group");
                            string having = GetParam("having");
                            string order = GetParam("order");
                            DataTable dt = null;
                            int totalCount = 0;
                            int ipage = int.Parse(GetParam("pagenow"));
                            int irows = int.Parse(GetParam("pagesize"));

                            using (dbsimple db = new dbsimple())
                            {
                                if (ipage != 0) dt = db.getReportPage(table, select, ipage, irows, where, string.IsNullOrEmpty(order) ? null : order, ref totalCount);
                                else
                                {
                                    dt = db.getReportNoPage(table, select, where, string.IsNullOrEmpty(order) ? null : order);
                                }
                            }
                            if (ipage != 0)
                            {
                                string allpage = "0";

                                if (totalCount != 0) allpage = (((totalCount - 1) / irows) + 1).ToString();
                                Mcontext.Response.Write(JsonControl.AjaxJsonResult(allpage, JsonControl.GetJsonStr(dt)));
                            }
                            else
                            { Mcontext.Response.Write(JsonControl.AjaxJsonResult(dt.Rows.Count.ToString(), JsonControl.GetJsonStr(dt))); }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.LogError("spui_" + callBackAction, ex);
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult(JsonControl.Error, JsonControl.GetJsonStr(ex.Message)));
                        }
                        break;
                    }
                case "doadd":
                    try
                    {
                        string erinfo = "";
                        string tablename = GetParam("tablename");
                        string fnamevalue = GetParam("fnamevalue");
                        Hashtable allinfo = new Hashtable();
                        allinfo.Add("tablename", tablename);
                        //"empid%" + $("#id").val() + "$roleid%" + $("#nofRoleinfo").combobox('getValue') + "$"
                        string[] nv = fnamevalue.Split('$');
                        foreach (var s in nv)
                        {
                            allinfo.Add("f" + s.Split('%')[0], s.Split('%')[1]);
                        }
                        bool re = ObjIUD.insertInfo(allinfo, out erinfo);
                        if (re)
                        {
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("OK", JsonControl.GetJsonStr("添加成功")));
                        }
                        else
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("NO", JsonControl.GetJsonStr(erinfo)));

                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogError("spui_doadd", ex);
                        Mcontext.Response.Write(JsonControl.AjaxJsonResult("ER", JsonControl.GetJsonStr(ex.Message)));
                    }

                    break;
                case "edit":
                    try
                    {
                        string id = "";
                        id = GetParam("id");
                        if (string.IsNullOrEmpty(id))
                        {
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("NO", JsonControl.GetJsonStr("请先选择要修改的记录，主键缺失！")));
                            break;
                        }
                        string erinfo = "";
                        Hashtable allinfo = new Hashtable();
                        string tablename = GetParam("tablename");
                        string fnamevalue = GetParam("fnamevalue");
                        string tablemidname = GetParam("tablemidname");
                        allinfo.Add("tablename", tablename);
                        allinfo.Add("id", id);
                        allinfo.Add("tablemidname", tablemidname);
                        string[] nv = fnamevalue.Split('$');
                        foreach (var s in nv)
                        {
                            allinfo.Add("f" + s.Split('%')[0], s.Split('%')[1]);
                        }

                        bool re = ObjIUD.updateInfo(allinfo, out erinfo);

                        if (re)
                        {
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("OK", JsonControl.GetJsonStr("更新成功")));
                        }
                        else
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("NO", JsonControl.GetJsonStr(erinfo)));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogError("spui_" + callBackAction, ex);
                        Mcontext.Response.Write(JsonControl.AjaxJsonResult("ER", JsonControl.GetJsonStr(ex.Message)));
                    }
                    break;
                case "del":
                    try
                    {
                        string erinfo = "";
                        Hashtable allinfo = new Hashtable();
                        for (int i = 0; i < Request.QueryString.Count; i++)
                        {
                            allinfo.Add(Request.QueryString.Keys[i].ToString(), Request.QueryString[i].ToString());
                        }

                        bool re = ObjIUD.delInfo(allinfo, out erinfo);

                        if (re)
                        {
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("OK", JsonControl.GetJsonStr("删除成功")));
                        }
                        else
                            Mcontext.Response.Write(JsonControl.AjaxJsonResult("NO", JsonControl.GetJsonStr(erinfo)));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogError("spui_" + callBackAction, ex);
                        Mcontext.Response.Write(JsonControl.AjaxJsonResult("ER", JsonControl.GetJsonStr(ex.Message)));
                    }

                    break;
                default:
                    {
                        Hashtable allinfo = new Hashtable();
                        for (int i = 0; i < Request.Form.Count; i++)
                        {

                            allinfo.Add(Request.Form.Keys[i].ToString(), Request.Form[i].ToString());
                        }
                        DataTable dt = new DataTable();
                        dt.Columns.Add("id", System.Type.GetType("System.String"));
                        dt.Columns.Add("name", System.Type.GetType("System.String"));
                        dt.Columns.Add("age", System.Type.GetType("System.String"));
                        DataRow dr = dt.NewRow();
                        dr["id"] = "1";
                        dr["name"] = "何潮";
                        dr["age"] = "30";
                        dt.Rows.Add(dr);
                        dr = dt.NewRow();
                        dr["id"] = "2";
                        dr["name"] = "是你";
                        dr["age"] = "20";
                        dt.Rows.Add(dr);
                        dr = dt.NewRow();
                        dr["id"] = "3";
                        dr["name"] = "就是你吗";
                        dr["age"] = "27";
                        dt.Rows.Add(dr);
                        Mcontext.Response.Write(JsonControl.AjaxJsonResult("OK", JsonControl.GetJsonStr(dt)));
                        break;
                    }
            }



        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }










    }
}